package com.neudu.yiyang.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.pagehelper.PageInfo;
import com.neudu.yiyang.mapper.BackdownMapper;
import com.neudu.yiyang.mapper.CustomerMapper;
import com.neudu.yiyang.pojo.Backdown;
import com.neudu.yiyang.pojo.Customer;
import com.neudu.yiyang.service.BackdownService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class BackdownServiceImpl extends ServiceImpl<BackdownMapper, Backdown> implements BackdownService {

    @Autowired
    private BackdownMapper backdownMapper;
    @Autowired
    private CustomerMapper customerMapper;
//
//    // 获取所有退住信息并关联客户姓名，支持分页查询
//    @Override
//    public PageInfo<Backdown> getAllBackdownsWithCustomerName(int pageNum, int pageSize) {
//        Page<Backdown> page = new Page<>(pageNum, pageSize);
//        IPage<Backdown> iPage = backdownMapper.selectPageAllBackdownsWithCustomerName(page, pageSize, (pageNum - 1) * pageSize);
//        return new PageInfo<>(iPage.getRecords());
//    }
//
//    // 根据关键字获取退住信息并关联客户姓名，支持分页查询
//    @Override
//    public PageInfo<Backdown> getBackdownsByKeywordWithCustomerName(String keyword, int pageNum, int pageSize) {
//        Page<Backdown> page = new Page<>(pageNum, pageSize);
//        IPage<Backdown> iPage = backdownMapper.selectPageBackdownsByKeywordWithCustomerName(page, keyword, pageSize, (pageNum - 1) * pageSize);
//        return new PageInfo<>(iPage.getRecords());
//    }

    // 根据退住信息 ID 获取退住信息
    @Override
    public Backdown getBackdownById(Integer id) {
        return backdownMapper.selectById(id);
    }


    // 添加一条退住信息
    @Override
    public void addBackdown(Backdown backdown) {
        backdownMapper.insert(backdown);
        // 新增退住信息时，将 customer 表中对应客户的 isRetreated 设为 1
        Customer customer = new Customer();
        customer.setId(backdown.getCustomerId());
        customer.setStatus(1);
        customerMapper.updateById(customer);
    }
    // 根据客户 ID 更新床位状态
    @Override
    public void updateBedStatus(int customerId, int status) {
        int bedId = backdownMapper.getBedIdByCustomerId(customerId);
        backdownMapper.updateBedStatus(bedId, status);
    }

    @Override
    public void updateBackdown(Backdown backdown) {
        backdownMapper.updateById(backdown);
    }
//    // 根据退住信息 ID 删除退住信息
//    @Override
//    public void deleteBackdownById(Integer id) {
//        backdownMapper.deleteById(id);
//    }
    @Override
    public Backdown getBackdownByCustomerId(Integer customerId) {
        return backdownMapper.selectByCustomerId(customerId);
    }

    @Override
    public void updateBackdownByCustomerId(Backdown backdown) {
        backdownMapper.updateByCustomerId(backdown);
    }

}